package com.woniu.airent.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.airent.dto.ArticleDto;
import com.woniu.airent.dto.ReplyDto;
import com.woniu.airent.entity.User;
import com.woniu.airent.service.ArticleService;
import com.woniu.airent.utils.TXYunUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;

@Controller
@RequestMapping("article")
public class ArticleController {
    @Autowired
    ArticleService articleService;

//    添加(发帖)
    @RequestMapping("/postArticle")
    @ResponseBody
    public String addArticle(HttpServletRequest request, @RequestBody ArticleDto article){
//        因为没有用户所以会报空指针
        /*User user = (User) request.getSession().getAttribute("user");
        article.setUserId(user.getUserId());*/

        article.setArticleTime(new Date());
        article.setArticleState(0);
        System.out.println(article);

//        添加
        int i = articleService.addArticle(article);

        return "success";


    }

//    条件查询
    @RequestMapping("/query")
    @ResponseBody
    public PageInfo<ArticleDto> queryArticle(@RequestParam(defaultValue = "1") int pageNum,
                                             @RequestParam(defaultValue = "2") int pageSize,
                                             String keyWord,
                                             @RequestParam(required = true) int status){
        PageInfo<ArticleDto> pageInfo = articleService.query(pageNum,pageSize,keyWord,status);
        System.out.println(pageInfo);
        return pageInfo;
    }

    //单个帖子查询
    @RequestMapping("/singleQuery")
    @ResponseBody
    public ArticleDto singleQuery(int id){
        ArticleDto articleDto = articleService.singleQuery(id);
        return articleDto;
    }

//    回帖
    @RequestMapping("/reply")
    public void replyArticle(HttpServletRequest request, @RequestBody ReplyDto replyDto){
//        User user = (User) request.getSession().getAttribute("user");
//        replyDto.setFromId(user.getUserId());
        replyDto.setFromId(1);

        articleService.addReply(replyDto);
    }

    @PostMapping(value = "/addImg")
    @ResponseBody
    public Map<String, Object> upload(@RequestParam("files") MultipartFile multipartFile, Model model) throws Exception {
        //获取文件的名称
        String fileName = multipartFile.getOriginalFilename();

        //获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));

        //使用uuid作为文件名，防止生成的临时文件重复
        final File excelFile = File.createTempFile("imagesFile-" + System.currentTimeMillis(), prefix);
        //将Multifile转换成File
        multipartFile.transferTo(excelFile);

        //调用腾讯云工具上传文件
        String imageName = TXYunUtil.uploadfile(excelFile, "avatar");

        //程序结束时，删除临时文件
        deleteFile(excelFile);

        //存入图片名称，用于网页显示
//        model.addAttribute("imageName", imageName);

        Map<String, Object> map = new HashMap<>();
        List<String> data = new ArrayList<>();
        data.add("https://airent-1303816068.cos.ap-shanghai.myqcloud.com/"+imageName);
        map.put("errno",0);
        /*map.put("data",data);*/
        map.put("url",imageName);
        System.out.println(map);

        //返回成功信息
        return map;
    }

    private void deleteFile(File... files) {
        for (File file : files) {
            if (file.exists()) {
                file.delete();
            }
        }
    }

    @RequestMapping("/delImg")
    public void delImg(String[] imgs){
        for (int i = 0; i < imgs.length; i++) {
            TXYunUtil.deletefile(imgs[i]);
        }
    }

}
